package com.sofun.ai.service.impl;


import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sofun.ai.service.GaodeWeatherService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 高德天气api
 * @author:wjl
 * @see:
 * @since:
 * @date:2025-03-04-22:58
 * @description:com.study.langchain4jspringboot.service
 * @version:1.0
 */
@Service
public class GaodeWeatherServiveImpl implements GaodeWeatherService{

    private final ObjectMapper objectMapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    @Value("${gaode.baseUrl}")
    private String baseUrl;

    @Value("${gaode.api-key}")
    private String apiKey;

    @Override
    public List<Forecasts> getWeather(String city) {
        //高德天气api
        String url = baseUrl + "?key=" + apiKey + "&city=" + city + "&extensions=all";
        String resp = HttpUtil.get(url);
        WeatherApiResponse weatherApiResponse = null;
        try {
            weatherApiResponse = objectMapper.readValue(resp, WeatherApiResponse.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        if ("0".equals(weatherApiResponse.status())) {
            throw new RuntimeException("获取高德天气信息失败！");
        }
        return weatherApiResponse.forecasts();
    }
    
}
